//
//  BaseNetManager.m
//  MyPhoneApp
//
//  Created by uuu9 on 16/9/8.
//  Copyright © 2016年 myApp. All rights reserved.
//

#import "BaseNetManager.h"
#import "sys/utsname.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "U9CustomeError.h"
#import "U9EncryptManager.h"
#import <AdSupport/AdSupport.h>

static AFHTTPSessionManager *manager;
static NSString *secdata;

@implementation BaseNetManager

+ (AFHTTPSessionManager *)sharedInstance
{
    static dispatch_once_t t;
    dispatch_once(&t, ^{
        manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        //                manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //                manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [manager.requestSerializer setTimeoutInterval:15.0];
        
    });
    return manager;
}


+ (void)GET:(NSString *)path parameters:(NSDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete{
    if (!secdata) {
        [self getSecData];
    }
    NSString *newpath = [NSString stringWithFormat:@"%@&param=%@",path,secdata];
    [[self sharedInstance] GET:newpath
                    parameters:nil
                      progress:nil
                       success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        id dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        complete(dict, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        complete(nil, error);
    }];
}

+ (void)POST:(NSString *)path parameters:(NSMutableDictionary *)params completionHandler:(void(^)(id responseObj, NSError *error))complete{
    if (!secdata) {
        [self getSecData];
    }
    [params setValue:secdata forKey:@"param"];
    [[self sharedInstance] POST:path
                     parameters:params
                       progress:nil
                        success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        id dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        complete(dict, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        complete(nil, error);
    }];
}


+ (void)getSecData{
    UIDevice *device = [[UIDevice alloc] init];
    NSString *systemVersion = device.systemVersion;
    NSString *systemName = device.systemName;
    NSString *nowVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSString *str = [NSString stringWithFormat:@"_version=%@&_appversion==%@&_systemtype=%@&_idfa=%@",systemVersion,nowVersion,systemName,idfa];
    NSData* originData = [str dataUsingEncoding:NSASCIIStringEncoding];
    NSString* encodeResult = [originData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    secdata = encodeResult;
}


#pragma mark base64加密
+ (NSString *)base64Encoding:(NSString *)encoingStr{
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSData *data = [encoingStr dataUsingEncoding:NSUTF8StringEncoding];
    NSString *baseStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return baseStr;
}

+ (NSString *)base64Decoding:(NSString *)decodingStr{
    NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:decodingStr options:0];
    NSString *base64Decoded = [[NSString alloc] initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
    return base64Decoded;
}



+ (NSError *)createError:(NSString *)str{
    return [U9CustomeError createError:str];
}


@end
